package com.example.kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * ClassName: CustomProducer
 * Package: com.example.kafka
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-30
 * Time: 14:47
 */

//自定义生产者
public class CustomProducerAcks {
    public static void main(String[] args) {
        //属性配置
        Properties properties = new Properties();

        //走到这里之后 还没和集群产生联系
        //连接集群
        //也可以写成 "bootstrap.servers"
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "hadoop102:9092,hadoop103:9092");

        //指定对应的key和val序列化类型 StringSerializer字符串序列化程序
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());


        //设置应答级别 默认值是all 值就是-1
        properties.put(ProducerConfig.ACKS_CONFIG,"1");

        //设置重试次数 默认最大次数是int的最大值 2147483647
        properties.put(ProducerConfig.RETRIES_CONFIG,3);



        //1.创建Kafka生产者
        //数据类型 "" hello
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties);

        //2.发送数据
        for (int i = 0; i < 5; i++) {
            //最普通的send方法 直接方法到那个主题 和 数据
            kafkaProducer.send(new ProducerRecord<>("first", "hello" + i));
        }
        //3.关闭资源
        kafkaProducer.close();
    }

}
